package day31;

import java.util.concurrent.*;

/**
 * @author: 王丹
 * @create: 2021-07-26 23:26
 * * 方式3：ExecutorService.submit方法实现:等待线程完成的方式
 **/
public class Demo03 {
    /**
     * 使用ExecutorService.submit方法实现的，此方法返回一个Future，future.get()会让当前线程阻塞，
     * 直到Future关联的任务执行完毕。
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<Integer> future = executorService.submit(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        });
        executorService.shutdown();
        System.out.println(System.currentTimeMillis());
        Integer result = future.get();
        System.out.println(System.currentTimeMillis() + ":" + result);


    }
}
